package com.ruoyi.web.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.config.pay.CommonFieldName;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.web.request.IESI123102Request;
import com.ruoyi.web.service.AgentPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.PaymentLimitResultMapper;
import com.ruoyi.web.domain.PaymentLimitResult;
import com.ruoyi.web.service.IPaymentLimitResultService;
import com.ruoyi.common.core.text.Convert;

/**
 * 代付限额申请查询Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-04
 */
@Service
public class PaymentLimitResultServiceImpl implements IPaymentLimitResultService 
{
    @Autowired
    private PaymentLimitResultMapper paymentLimitResultMapper;
    @Autowired
    private AgentPayService agentPayService;
    /**
     * 查询代付限额申请查询
     * 
     * @param sequenceNo 代付限额申请查询ID
     * @return 代付限额申请查询
     */
    @Override
    public PaymentLimitResult selectPaymentLimitResultById(String sequenceNo)
    {
        return paymentLimitResultMapper.selectPaymentLimitResultById(sequenceNo);
    }

    /**
     * 查询代付限额申请查询列表
     * 
     * @param paymentLimitResult 代付限额申请查询
     * @return 代付限额申请查询
     */
    @Override
    public List<PaymentLimitResult> selectPaymentLimitResultList(PaymentLimitResult paymentLimitResult)
    {
        return paymentLimitResultMapper.selectPaymentLimitResultList(paymentLimitResult);
    }

    /**
     * 新增代付限额申请查询
     * 
     * @param paymentLimitResult 代付限额申请查询
     * @return 结果
     */
    @Override
    public int insertPaymentLimitResult(PaymentLimitResult paymentLimitResult) throws Exception
    {
        IESI123102Request request =new IESI123102Request();
        request.setApplyNo(paymentLimitResult.getApplyNo());
        paymentLimitResult.setSequenceNo(paymentLimitResult.getApplyNo());
        HashMap<String, Object> resultMap= agentPayService.IESI123102(request,paymentLimitResult.getApplyNo(),paymentLimitResult.getMerchCd());
        String respCode = (String) resultMap.get(CommonFieldName.RESP_CODE);
        String respMsg = (String) resultMap.get(CommonFieldName.RESP_MSG);
        if (!"00000".equals(respCode)){
            BusinessException.notNull(null,respMsg);
        }
        paymentLimitResult.setStatus(respCode);
        paymentLimitResult.setApplyDate((String)resultMap.get("applyDate"));
        paymentLimitResult.setApplyTime((String)resultMap.get("applyTime"));
        paymentLimitResult.setCnt((Integer) resultMap.get("cnt"));
        if (resultMap.get("amt")!=null){
            paymentLimitResult.setAmt(new BigDecimal((String)resultMap.get("amt")));
        }
        paymentLimitResult.setMode((String) resultMap.get("mode"));
        paymentLimitResult.setExtRef((String) resultMap.get("extRef"));
        paymentLimitResult.setExtStat((String) resultMap.get("extStat"));
        paymentLimitResult.setExtInfo((String) resultMap.get("extInfo"));
        if (resultMap.get("quotaBal")!=null){
            paymentLimitResult.setQuotaBal(new BigDecimal((String)resultMap.get("quotaBal")));
        }
        paymentLimitResult.setMsg(respMsg);
        PaymentLimitResult old =selectPaymentLimitResultById(paymentLimitResult.getApplyNo());
        if (old!=null){
            return paymentLimitResultMapper.updatePaymentLimitResult(paymentLimitResult);
        }
        return paymentLimitResultMapper.insertPaymentLimitResult(paymentLimitResult);
    }

    /**
     * 修改代付限额申请查询
     * 
     * @param paymentLimitResult 代付限额申请查询
     * @return 结果
     */
    @Override
    public int updatePaymentLimitResult(PaymentLimitResult paymentLimitResult)
    {
        return paymentLimitResultMapper.updatePaymentLimitResult(paymentLimitResult);
    }

    /**
     * 删除代付限额申请查询对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaymentLimitResultByIds(String ids)
    {
        return paymentLimitResultMapper.deletePaymentLimitResultByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除代付限额申请查询信息
     * 
     * @param sequenceNo 代付限额申请查询ID
     * @return 结果
     */
    @Override
    public int deletePaymentLimitResultById(String sequenceNo)
    {
        return paymentLimitResultMapper.deletePaymentLimitResultById(sequenceNo);
    }
}
